﻿using ImageProcessing.Model.LogModel;

using Microsoft.Extensions.Logging;

using Newtonsoft.Json;

namespace ImageProcessing.Comm.LogHelp
{
    /// <summary>
    /// 日志帮助实现类
    /// ErLin Li
    /// 2021/4/15
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class LoggerHelper<T> : ILoggerHelper<T>
    {
        private readonly LogModel _logModel;
        private readonly LogModel_Message _logModelMessage;
        private readonly ILogger<LoggerHelper<T>> _logger;

        public LoggerHelper(
            LogModel logModel,
            LogModel_Message logModelMessage,
            ILogger<LoggerHelper<T>> logger)
        {
            _logModel = logModel;
            _logModelMessage = logModelMessage;
            _logger = logger;
        }

        private bool is_log_record_local_file;

        //private readonly ConcurrentDictionary<Type, ILog> Loggers = new ConcurrentDictionary<Type, ILog>();
        ///// <summary>
        ///// 获取记录器
        ///// </summary>
        ///// <param name="source">soruce</param>
        ///// <returns></returns>
        //private ILog GetLogger(Type source)
        //{
        //    Console.WriteLine("Loggers:" + Loggers);
        //    Console.WriteLine("source:" + source);
        //    if (Loggers.ContainsKey(source))
        //    {
        //        return Loggers[source];
        //    }
        //    var programname = Process.GetCurrentProcess().MainModule.FileName.Split('\\')[Process.GetCurrentProcess().MainModule.FileName.Split('\\').Length - 1];
        //    ILog logger = LogManager.GetLogger(programname.Split('.')[0], source);//Startup.repository.Name
        //    Loggers.TryAdd(source, logger);
        //    return logger;
        //}

        //#region debug

        ///// <summary>
        ///// 调试信息
        ///// </summary>
        ///// <param name="source">source</param>
        ///// <param name="message">message</param>
        //public void Debug(LogModel logModel)
        //{
        //    ILog logger = GetLogger(typeof(T));
        //    if (!logger.IsDebugEnabled)
        //    {
        //        return;
        //    }
        //    logModel.s_level = "Debug";
        //    var message = JsonConvert.SerializeObject(logModel);
        //    Console.WriteLine(message);
        //}

        //#endregion
        ///// <summary>
        ///// 调试信息
        ///// </summary>
        ///// <param name="source">source</param>
        ///// <param name="message">message</param>
        //public void Debug(string message)
        //{
        //    GetLogModelInfo(typeof(T), message);
        //    Debug(_logModel);
        //}

        #region info

        /// <summary>
        /// 关键信息
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="logModel">message</param>
        public void Info(LogModel logModel)
        {
            Console.WriteLine("Console.WriteLine测试打印");
            _logger.LogInformation("ILogger打印");
            var message = JsonConvert.SerializeObject(logModel) + ",";
            if (!is_log_record_local_file)
            {
                Console.WriteLine(message);
                return;
            }
            _logger.LogInformation(message);
        }

        public void GlobalActionFilter_Log(LogModel logModel)
        {
            Info(logModel);
        }

        public void Info(string message)
        {
            GetLogModelInfo(typeof(T), message);
            Info(_logModel);
        }

        #endregion info

        #region warn

        /// <summary>
        /// 警告信息
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="logModel">logModel</param>
        public void Warn(LogModel logModel)
        {
            logModel.s_level = "Warn";
            var message = JsonConvert.SerializeObject(logModel) + ",";
            if (!is_log_record_local_file)
            {
                Console.WriteLine(message);
                return;
            }
            _logger.LogWarning(message);
        }

        public void Warn(string message)
        {
            GetLogModelInfo(typeof(T), message);
            Warn(_logModel);
        }

        public void Warn(string message, Exception exception)
        {
            GetLogModelInfo(typeof(T), message, exception);
            Warn(_logModel);
        }

        #endregion warn

        #region error

        /// <summary>
        /// 错误信息
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="logModel">logModel</param>
        public void Error(LogModel logModel)
        {
            var message = JsonConvert.SerializeObject(logModel) + ",";
            if (!is_log_record_local_file)
            {
                Console.WriteLine(message);
                return;
            }
            _logger.LogError(message);
        }

        public void GlobalExceptionFilter_Log(LogModel logModel)
        {
            Error(logModel);
        }

        public void Error(string message, Exception exception)
        {
            GetLogModelInfo(typeof(T), message, exception);
            Error(_logModel);
        }

        public void Error(string message)
        {
            GetLogModelInfo(typeof(T), message);
            Error(_logModel);
        }

        #endregion error

        #region fatal

        /// <summary>
        /// 失败信息
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="message">message</param>
        public void Fatal(LogModel logModel)
        {
            var message = JsonConvert.SerializeObject(logModel) + ",";
            if (!is_log_record_local_file)
            {
                Console.WriteLine(message);
                return;
            }
            _logger.LogCritical(message);
        }

        /* Log a message object and exception */

        public void Fatal(string message, Exception exception)
        {
            GetLogModelInfo(typeof(T), message, exception);
            Fatal(_logModel);
        }

        #endregion fatal

        #region help

        /// <summary>
        /// 获取GetLogModelInfo
        /// </summary>
        /// <param name="Headers"></param>
        public void GetLogModelInfo(Type source, string message)
        {
            if (_logModel.log_message == null)
            {
                LogModel_Message logModelMessage = new LogModel_Message();
                _logModel.log_message = logModelMessage;
            }
            _logModel.log_message.action = "actionExecuting";
            _logModel.class_name = source.GetType().FullName;
            _logModel.log_message.handwriting_info = message;
            //_logModelMessage.sys_info = $"Method:{context.HttpContext.Request.Method},Path:{context.HttpContext.Request.Path},QueryString:{context.HttpContext.Request.QueryString}";
            _logModel.thread_name = $"线程ID:{Thread.CurrentThread.ManagedThreadId.ToString()}-线程名称:{Thread.CurrentThread.Name}";
            _logModel.dt_time = DateTime.Now;
        }

        /// <summary>
        /// 获取GetLogModelInfo
        /// </summary>
        /// <param name="Headers"></param>
        public void GetLogModelInfo(Type source, string message, Exception exception)
        {
            _logModel.class_name = source.FullName;
            if (_logModel.log_message == null)
            {
                return;
            }
            _logModel.log_message.handwriting_info = message;
            _logModel.log_message.sys_info = exception;
            //_logModelMessage.sys_info = $"Method:{context.HttpContext.Request.Method},Path:{context.HttpContext.Request.Path},QueryString:{context.HttpContext.Request.QueryString}";
            _logModel.thread_name = $"线程ID:{Thread.CurrentThread.ManagedThreadId.ToString()}-线程名称:{Thread.CurrentThread.Name}";
            _logModel.dt_time = DateTime.Now;
        }

        #endregion help
    }
}